'''
2)参考1.09的综合案例中的学生信息管理，来实现一个自动取款机的存取款模拟效果。
要求有登陆和退出、查询余额、取钱，存钱等操作
'''
# 通过字典保存账号密码
bak = [{'card_nbr':'0000','password':'0000','balances':1000},
        {'card_nbr':'1234','password':'1234','balances':2000},
        {'card_nbr':'5678','password':'5678','balances':3000},
        {'card_nbr':'9999','password':'9999','balances':5000},
        ]

# 1.主体框架
def mainframe():
    '''
    主体框架，包含：1.查询余额,2.存款,3.取款
    '''
    for i in range(len(bak)):
        if bak[i]['card_nbr'] == card_nbr and bak[i]['password'] == password:
            print("="*41)
            print("{0:1}{1:<12}{2:<16}{3:<10}".format(" ","1.查询余额","2.存款","3.取款"))
            print("="*41)
            key1 = input("请选择服务项目：")
            if key1 == '1':
                Check_balances(card_nbr)
                input("按Enter键继续...")
                break
                #return 1
            elif key1 == '2':
                deposit(card_nbr)
                break
                #return 1
            elif key1 == '3':
                Withdraw(card_nbr)
                break
                #return 1
    else:
        print("银行卡号或密码错误!!!")
    print("请选择后续服务：")
    print("="*41)
    print("{:<20}{:<20}".format(" 1.返回上一级","2.退出"))
    print("="*41)
    key2 = input("请选择服务项目：")
    if key2 == '1':
        mainframe() # 调用函数本身
        return
    else:
        return

# 2.查询余额
def Check_balances(cardnbr): 
    '''
    查询余额
    ''' 
    # cardnbr='5678'
    for i in range(len(bak)):
        if bak[i]['card_nbr'] == cardnbr:
            print("="*41)
            print("{:^15}|{:^15}".format('银行卡号','账户余额'))
            print("{:^19}|{:^19}".format(bak[i]['card_nbr'],bak[i]['balances']))
            print("="*41)
    return

# 3.存款
def deposit(cardnbr):
    '''
    存款函数
    '''
    # 显示余额
    print("当前余额：")
    Check_balances(cardnbr)
    # 输入存钱金额
    cash_in = float(input('请输入存款金额：'))
    input("您将存款：{}元 按Enter键继续...".format(cash_in))
    for i in range(len(bak)):
        if bak[i]['card_nbr'] == cardnbr:
            bak[i]['balances']=bak[i]['balances']+cash_in
            # 存款成功，显示余额
            print("存款成功!!! 当前余额：")
            Check_balances(cardnbr) 
            input("按Enter键继续...")
    return

# 4.取款
def Withdraw(cardnbr):
    '''
    取款函数
    '''
    # 显示余额
    print("当前余额：")
    Check_balances(cardnbr)
    # 输入取钱金额
    cash_out = float(input('请输入取款金额：'))
    input("您将取款：{}元 按Enter键继续...".format(cash_out))
    for i in range(len(bak)):
        if bak[i]['card_nbr'] == cardnbr and bak[i]['balances'] >= cash_out:    
            bak[i]['balances']=bak[i]['balances'] - cash_out
            # 取款成功，显示余额
            print("取款成功!!! 当前余额：")
            Check_balances(cardnbr)
            input("按Enter键继续...")
            break
    else:
        # 取款失败，报错并显示余额
        print("余额不足!!! 当前余额：")
        Check_balances(cardnbr)
        input("按Enter键继续...")
    return


# 主程序
while True:
    # 输出初始界面
    print()
    print("{:=^40}".format(" 自动取款系统 "))
    print("{:^20}{:^20}".format("1.登陆","2.退出"))
    key = input("请输入序号：")
    #登陆
    if key =='1':
        global card_nbr
        card_nbr = input("请输入银行卡号：")
        password = input("请输入银行卡密码：")
        mainframe() # 调用主体框架函数
        break
    else:
        break